package com.accompany.fragment.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.accompany.R;
import com.accompany.api.bean.UserLoginBean;
import com.accompany.api.category.UserLoginCategory;
import com.accompany.api.presenter.UserLoginPresenter;
import com.accompany.base.BaseActivity;
import com.accompany.base.BaseBean;
import com.accompany.fragment.my.activity.ProtocolActivity;
import com.accompany.fragment.my.activity.ProtocolProtectActivity;
import com.accompany.main.BaseApplication;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.main.MainActivity;
import com.accompany.utils.CountDownTimerUtils;
import com.accompany.utils.SPUtils;
import com.accompany.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

/**
 * 信息说明: 登录界面
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/4/22 11 ：31
 * 项目名称：yuanban
 */
public class UserLoginActivity extends BaseActivity implements UserLoginCategory.View {
    @BindView(R.id.edit_phone)
    EditText mEditPhone;
    private String mMobile;
    @BindView(R.id.send_code)
    TextView mSendCode;
    @BindView(R.id.edit_psd)
    EditText mEditPsd;
    @BindView(R.id.check_box)
    CheckBox mCheckBox;
    @BindView(R.id.linear_areed)
    LinearLayout mLlAgreement;
    private UserLoginPresenter mPresenter;
    private String mUserCode, isOffer;
    private long exitTime = 0;
    @Override
    public int getLayoutId() {
        return R.layout.activity_user_login;
    }

    @Override
    public void initLayoutView() {
        //设置全透明沉浸式状态栏
        YCAppBar.translucentStatusBar(this, true);
        mPresenter = new UserLoginPresenter(this);
        SetPersenter(mPresenter);
        isOffer = (String) SPUtils.get(UserLoginActivity.this, "offerSuccess", "");
        if (!isOffer.equals("")) {
            openActivityWitchAnimation(MainActivity.class);
            finish();
        } else {
            mPresenter = new UserLoginPresenter(UserLoginActivity.this);
            SetPersenter(mPresenter);
        }
    }

    @OnClick({R.id.to_login, R.id.send_code,R.id.agreement,R.id.policy})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.to_login:
                uSerlogin();
                break;
            case R.id.send_code:
                mMobile = mEditPhone.getText().toString().trim();
                if (mMobile.equals("")) {
                    ToastUtil.showToastINFO(UserLoginActivity.this, "请输入手机号码");
                } else {
                    CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mSendCode, 60000, 1000);
                    mCountDownTimerUtils.start();
                    mPresenter.getUserCode(mMobile);
                }
                break;
            case R.id.agreement://
                openActivityWitchAnimation(ProtocolActivity.class);
                break;
            case R.id.policy://
                openActivityWitchAnimation(ProtocolProtectActivity.class);
                break;
        }
    }

    private void uSerlogin() {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        mMobile = mEditPhone.getText().toString().trim();
        mUserCode = mEditPsd.getText().toString().trim();
        if (mMobile.equals("")) {
            ToastUtil.showToastINFO(UserLoginActivity.this, "请输入手机号码");
            return;
        } else if (!TextUtils.isEmpty(mMobile) && !mMobile.matches(regex)) {
            ToastUtil.showToastINFO(UserLoginActivity.this, "手机号码输入有误,请重新输入");
            mEditPhone.setText("");
            return;
        } else if (mUserCode.equals("")) {
            ToastUtil.showToastINFO(UserLoginActivity.this, "请输入验证码");
            return;
        } else if (mCheckBox.isChecked() == false) {
            mLlAgreement.startAnimation(AnimationUtils.loadAnimation(this, R.anim.transla_checkbox));
            return;
        } else {
            mPresenter.uSerLogin(mMobile, mUserCode);
            return;
        }
    }
    @Override
    public void getUserCode(BaseBean userCode) {
        if (userCode.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            ToastUtil.showToastSUCCESS(UserLoginActivity.this, "验证码获取成功");
        } else {
            ToastUtil.showToastINFO(UserLoginActivity.this, userCode.getMsg());
        }
    }
    @Override
    public void uSerLogin(UserLoginBean loginBean) {
        if (loginBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            String mLoginMsg = getIntent().getStringExtra("loginOUt");
            //从其他页面进入登录页面  需要返回
            if (mLoginMsg != null && mLoginMsg.equals("0")) {
                //保存登录接口返回的  是否已经认证的状态
                SPUtils.put(UserLoginActivity.this, "isIdentification", loginBean.getData().getAcd().getIsIdentification());
                SPUtils.put(UserLoginActivity.this, ConstantStaticUrl.mToken, loginBean.getData().getToken());

                SPUtils.put(UserLoginActivity.this, "offerSuccess", "success");
                SPUtils.put(UserLoginActivity.this, "account", loginBean.getData().getAcd().getAccount());

                SharedPreferences sharedPreferences = getSharedPreferences("First", MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putBoolean("isFirst", false);//是否第一次进入  登录
                edit.putBoolean("isLogin", true);
                edit.putLong(ConstantStaticUrl.userId, loginBean.getData().getUserId());
                edit.putString(ConstantStaticUrl.userId, String.valueOf(loginBean.getData().getUserId()) );
                edit.commit();
                //数据是使用Intent返回
                Intent intent = new Intent();
                intent.putExtra(ConstantStaticUrl.mToken, loginBean.getData().getToken());
                intent.putExtra(ConstantStaticUrl.userId, loginBean.getData().getUserId());
                //设置返回数据
                UserLoginActivity.this.setResult(400, intent);//RESULT_OK为自定义常量
                openActivityWitchAnimation(MainActivity.class);
                //关闭Activity
                UserLoginActivity.this.finish();
            } else {//从 登录页登录
                //保存登录接口返回的  是否已经认证的状态
                SPUtils.put(UserLoginActivity.this, "isIdentification", loginBean.getData().getAcd().getIsIdentification());
                //保存token
                SPUtils.put(UserLoginActivity.this, "account", loginBean.getData().getAcd().getAccount());
                SPUtils.put(UserLoginActivity.this, ConstantStaticUrl.mToken, loginBean.getData().getToken());
                SPUtils.put(UserLoginActivity.this, "offerSuccess", "success");
                SharedPreferences sharedPreferences = getSharedPreferences("First", MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putBoolean("isFirst", false);
                edit.putLong(ConstantStaticUrl.userId, loginBean.getData().getUserId());
                edit.putBoolean("isLogin", true);
                edit.commit();
                openActivityWitchAnimation(MainActivity.class);
                finish();
            }
        } else {
            ToastUtil.showToastINFO(UserLoginActivity.this, loginBean.getMsg());
        }
    }



    @Override
    public void SetPersenter(Object persenter) {}

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(UserLoginActivity.this, mErrorMsg);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showToastERROR(UserLoginActivity.this, "再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                BaseApplication.removeALLActivity();
                System.exit(0);
            }
            return true;
        }
        return false;
    }

}
